import Axios from 'axios'
import Future from "./future.js";
import Config from '../config/axios.config.js'
import App from '../main.js'
import Vue from 'vue';
import { Confirm, Alert, Toast, Notify, Loading } from 'vue-ydui/dist/lib.rem/dialog';
var LoadingInstance = {};
// 数据请求之前
Axios.interceptors.request.use((config) => {
    // Loading.open('很快加载好了')
    var pattern = /user\/bank\/branch/;
    if (
        window.localStorage.token !== undefined &&
        window.localStorage.token !== 'undefined' &&
        window.localStorage.token !== '' &&
        window.localStorage.token !== null) {
        config.headers.token = window.localStorage.token;
    } else {
        console.info('token not found');
    }
    return config;
});


// 提交成功并返回数据时
Axios.interceptors.response.use(function (response) {
    Loading.close();
    // Toast({
    //     mes: response.data.msg,
    //     timeout: 1000,
    //     icon: 'info'
    // });
    return response;
}, function (error) {
    return Promise.reject(error);
});

class Ajax {
    static ResponseHandle(F, res) {
        switch (res.data.error) {
            case 1:
                Toast({
                    mes: res.data.msg,
                    icon: 'info'
                });
                F.reject(res.data.msg);
                break;
            case 2:
                window.localStorage.user_status = 'outlogin';
                App.$router.push('/login');
                break;
            default:
                F.resolve(res.data.data);
                break;
        }
    }
    static Post(url, data) {
        var F = new Future();
        Axios.post(url, data, Config)
            .then((res) => {
                Ajax.ResponseHandle(F, res);
            })
            .catch((e) => {
                F.reject(e);
            });
        return F;
    }
    static PostExel(url, data, Config) {
        var F = new Future();
        Axios.post(url, data, Config)
            .then((res) => {
                Ajax.ResponseHandle(F, res);
            })
            .catch((e) => {
                console.info(e);
                F.reject(e);
            });
        return F;
    }
    static Get(url) {
        var F = new Future();
        Axios.get(url, Config)
            .then((res) => {
                Ajax.ResponseHandle(F, res);
            })
            .catch((e) => {
                F.reject(e);
            });
        return F;
    }
    static Put(url, data) {
        var F = new Future();
        Axios.Put(url, data, Config)
            .then((res) => {
                Ajax.ResponseHandle(F, res);
            })
            .catch((e) => {
                console.info(e);
                F.reject(e);
            });
        return F;
    }
}
export default Ajax;